//
//  BFCategoryLabel.m
//  13KModel
//
//  Created by ISS on 22/09/12.
//
//
// CLASS INCLUDES
#import "BFCategoryLabel.h"

// CONST DEFINE
CGFloat const kTextFontSize = 28.0f;
CGFloat const kTimeIntervalValue = 0.2f;

@implementation BFCategoryLabel
@synthesize textLabel = _textLabel;
@synthesize backgroundView = _backgroundView;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.clipsToBounds = YES;
        
        _backgroundView = [[UIImageView alloc] initWithFrame:self.bounds];
        _backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin |
        UIViewAutoresizingFlexibleTopMargin;
        _backgroundView.contentMode = UIViewContentModeScaleAspectFill;
        [self addSubview:_backgroundView];
        
        _textLabel = [[UILabel alloc] initWithFrame:self.bounds];
//        _textLabel.font = [UIFont boldSystemFontOfSize:kTextFontSize];
        _textLabel.font = [UIFont fontWithName:@"Arial Rounded MT Bold" size:kTextFontSize];
        _textLabel.textColor = [UIColor yellowColor];
        _textLabel.backgroundColor = [UIColor clearColor];
        _textLabel.textAlignment = UITextAlignmentLeft;
        _textLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin |
        UIViewAutoresizingFlexibleTopMargin;
        [self addSubview:_textLabel];
    }
    return self;
}

- (void)updateTitleText:(NSString *)text toLeft:(BOOL)toLeft
{
    if (_textLabel.text && text) {
        [UIView animateWithDuration:kTimeIntervalValue animations:^{
            if (toLeft) {
                _textLabel.frame = CGRectMake(-self.bounds.size.width, 0, self.bounds.size.width, self.bounds.size.height);
            }
            else{
                _textLabel.frame = CGRectMake(self.bounds.size.width, 0, self.bounds.size.width, self.bounds.size.height);    
            }
                
        } completion:^(BOOL finished) {
            _textLabel.text = text;
            if (toLeft) {
                _textLabel.frame = CGRectMake(self.bounds.size.width, 0, self.bounds.size.width, self.bounds.size.height);
            }
            else{
                _textLabel.frame = CGRectMake(-self.bounds.size.width, 0, self.bounds.size.width, self.bounds.size.height);
            }
            [UIView animateWithDuration:kTimeIntervalValue animations:^{
                _textLabel.frame = self.bounds;
            }];
        }];
    }
    else{
        _textLabel.text = text;
    }
}

@end
